package com.example.app.pages.main.home.addNew.addRecord.MyModalBottomSheetRelationPicker

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.width
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.app.components.MyButton.MyTextButton
import com.example.app.components.MyDivider.MyDivider
import com.example.app.components.MyDivider.MyDividerDirection
import com.example.app.components.MyIcon.MyCircleAddIcon
import com.example.app.components.MyListItem.MyListItem
import com.example.app.components.MySpacer.MySpacer
import com.example.app.components.MyText.MyText
import com.example.app.pages.main.home.me.viewModel.AddNewViewModel
import com.example.app.ui.theme.my_border_color
import com.example.app.ui.theme.my_description_color

//联系人
//新增按钮
@Composable
fun ManageIncomeRelationAdd(
        clickHandler: () -> Unit,
        addNewViewModel: AddNewViewModel,
        showConfirmDialog: () -> Unit,
        selectAll: () -> Unit,
        isEdit: MutableState<Boolean>,
) {
    var scope = rememberCoroutineScope()
    var _isEdit by remember {
        isEdit
    }
    Column(
        modifier = Modifier
            .background(MaterialTheme.colorScheme.surface)
            .clickable {
                clickHandler()
            }
    ) {
        MyListItem(
            modifier = Modifier.fillMaxSize(),
            hasWrap = false,
            headlineContent = {
                Row(
                    horizontalArrangement = Arrangement.Start,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    MyCircleAddIcon(tint = my_description_color)
                    MySpacer(modifier = Modifier.width(4.dp))
                    MyText(
                        text = "新增关系人",
                        color = my_description_color
                    )

                    MySpacer(modifier = Modifier.weight(1f))

                    Row {
                        if (addNewViewModel.checkBoxListIncomeRelationPeopleItemList.filter { it.checked }.size > 0) {
                            MyTextButton(
                                onClick = showConfirmDialog,
                                content = {
                                    MyText(text = "删除")
                                }
                            )
                        }

                        if (_isEdit && addNewViewModel.checkBoxListIncomeRelationPeopleItemList.size > 0) {
                            var hasUnSelected =
                                addNewViewModel.checkBoxListIncomeRelationPeopleItemList.find { !it.checked }
                            MySpacer()
                            MyTextButton(
                                onClick = selectAll,
                                content = {
                                    MyText(text = if (hasUnSelected != null) "全选" else "反选")
                                }
                            )
                        }
                    }
                }
            },
            //containerColor的颜色，白色
            colors = ListItemDefaults.colors(
                containerColor = Color.Transparent
            )
        )
        MyDivider(
            myDividerDirection = MyDividerDirection.HOR,
            color = my_border_color
        )
    }
}